<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h2 class="login-title">大事件-文章管理系统</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-position="left"
        label-width="80px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="handleLogin"
            style="width: 100%"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
        <div class="register-link">
          还没有账号？<a href="#" @click.prevent="handleRegister">立即注册</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import useUserStore from '../store/user.js'

export default {
  name: 'Login',
  setup() {
    const loginFormRef = ref(null)
    const userStore = useUserStore()
    const loading = ref(false)
    
    const loginForm = ref({
      username: '',
      password: ''
    })
    
    const loginRules = ref({
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符之间', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符之间', trigger: 'blur' }
      ]
    })
    
    // 处理登录
    const handleLogin = async () => {
      try {
        // 表单验证
        await loginFormRef.value.validate()
        
        loading.value = true
        
        // 调用登录接口
        const result = await userStore.login(loginForm.value.username, loginForm.value.password)
        
        if (result.success) {
          ElMessage.success('登录成功')
          // 登录成功后跳转到首页
          window.location.href = '/'
        } else {
          ElMessage.error(result.message || '登录失败')
        }
      } catch (error) {
        console.error('登录错误:', error)
        if (error.name !== 'ValidateError') {
          ElMessage.error('登录失败，请重试')
        }
      } finally {
        loading.value = false
      }
    }
    
    // 处理注册链接点击
    const handleRegister = () => {
      // 跳转到注册页面
      window.location.href = '/register'
    }
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      handleLogin,
      handleRegister
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-form-wrapper {
  background-color: #fff;
  padding: 40px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 400px;
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.login-form {
  width: 100%;
}

.register-link {
  text-align: center;
  margin-top: 20px;
}

.register-link a {
  color: #409eff;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}
</style>